/*
 * Copyright (C) 2012 Tomas Shestakov. <http://code.google.com/p/jmmo/>
 */

package jmmo.engine.multifacetedactor

import akka.actor.ActorRef

trait ActorServices extends MultifacetedActor {

  def services: scala.collection.Map[Any, (ActorRef, Option[Any])] = servicesMap

  def addService(id: Any, ref: ActorRef, data: Option[Any]) {
    servicesMap.get(id) match {
      case Some(s) => //throw new IllegalArgumentException(this + " already contains service " + s + " with id " + id)
      case None =>
        servicesMap += (id -> (ref, data))
        sendEvent(ServiceAvailable(id: Any, ref: ActorRef, data: Option[Any]))
    }
  }

  def removeService(id: Any) {
    servicesMap.get(id) match {
      case Some((ref, data)) =>
        servicesMap -= id
        sendEvent(ServiceRevoked(id, ref, data))
      case None => //throw new IllegalArgumentException(this + " not contains servise with id " + id)
    }
  }

  override def receive = super.receive orElse {
    case AddServiceRequest(id, ref, data) => addService(id, ref, data)
    case RemoveServiceRequest(id) => removeService(id)
    case GetServicesRequest => sender ! GetServicesResponse(services)
  }

  protected var servicesMap = Map.empty[Any, (ActorRef, Option[Any])]
}
